﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace UILayoutEditor
{
    class ColorList
    {
        private Dictionary<string, Color> colors = new Dictionary<string, Color>();
        public Color GetColor(string name)
        {
            name = name.Trim();
            return colors[name];
        }
        public static ColorList Load(string filename)
        {
            ColorList newColorList = new ColorList();
            FileStream fs = new FileStream(filename, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            UInt32 entryCount = br.ReadUInt32();
            for (int i = 0; i < (int)entryCount; i++)
            {
                string colourName = ColorList.ReadLine(br);
                byte blue = br.ReadByte();
                byte green = br.ReadByte();
                byte red = br.ReadByte();
                br.ReadByte(); //Disregard extra byte
                newColorList.colors.Add(colourName, Color.FromArgb(red, green, blue));
            }
            br.Close();
            fs.Close();
            return newColorList;
        }
        static string ReadLine(BinaryReader br)
        {
            ushort stringLength = br.ReadUInt16();
            char[] charArray = br.ReadChars(stringLength - 1); // Read 1 less so that we don't catch the null terminator
            br.ReadByte(); // Now read and disregard the null terminator
            string line = new String(charArray);
           
            return line;
        }        
    }
}
